package com.comp490.sprite;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.comp490.shooter.GameView;
import com.comp490.shooter.MainActivity;
import com.comp490.sound.SoundManager;

public class ShipSprite extends Sprite implements Ship
{
    private int health;
    private int lives;
    private final int max_X;
    private final int max_Y;
    private final float collisionRadius;
    private long startDraw = 0;
    private ExplosionSprite mExplosionSprite;
    private static String GO = "GO!", READY = "GET READY!";

    public ShipSprite(GameView gameView)
    {
        super(gameView);

        Bitmap shipBitmap = BitmapResource.PLAYER_SHIP; 
        setBitmap(shipBitmap, shipBitmap.getWidth(), shipBitmap.getHeight());
        mExplosionSprite = new ExplosionSprite(gameView);
        collisionRadius = bmpHeight < bmpWidth ? bmpHeight / 4
                : bmpWidth / 4;

        max_X = getMax_X();
        max_Y = getMax_Y();
        health = 3;
        lives = 3;
        
    }

    public float collisionRadius()
    {
        return collisionRadius;
    }

    @Override
    public void draw(Canvas canvas)
    {
    	if(lives < 1)
    	{
    		if(explode)
    			explode = mExplosionSprite.draw(canvas, x, y, ExplosionSprite.EXPLOSION_TWO);
    		((MainActivity)gameView.getContext()).showScoreMenu(false);
    		return;
    	}
    		
        if(!explode)
            canvas.drawBitmap(bitmap, x, y, null);
        else if(!(explode = mExplosionSprite.draw(canvas, x, y, ExplosionSprite.EXPLOSION_TWO)))
        {
            reset();
        }
        
        if(!active && !gameView.levelCompleted)
        {
        	if(startDraw == 0)
        		startDraw = System.currentTimeMillis();
        	
        	long current = System.currentTimeMillis();
        	if((current - startDraw)/1000 >= 3)
        	{
        		canvas.drawText(GO, (max_X - BitmapResource.GAME_INDICATOR.measureText(GO))/2, max_Y/2, BitmapResource.GAME_INDICATOR);	
        	}
        	else
        	{
        		canvas.drawText(READY, (max_X - BitmapResource.GAME_INDICATOR.measureText(READY))/2, max_Y/2, BitmapResource.GAME_INDICATOR);
        	}
        	
        	if((current - startDraw) / 1000 >= 4)
        	{
        		active = true;
        		startDraw = 0;
        	}
        }
    }

    public void hit(int damage)
    {
        health -= damage;
        if(health < 1)
        {
            explode = true;
            active = false;           
            lives --;
            ((MainActivity)gameView.getContext()).runOnUiThread(new Runnable()
			{
				
				@Override
				public void run()
				{
					((MainActivity)gameView.getContext()).reduceLifeUI(lives+1);
				}
			});//remove specific view
            SoundManager.playSoundEfx(SoundManager.PLAYER_EXPLOSION);
        }
    }

    public float getTipX()
    {
        return x + bmpWidth;
    }

    public float getTipY()
    {
        return y + 24;
    }

    @Override
    public void setX(float x)
    {
        this.x = x > max_X ? max_X : x < 0 ? 0 : x;
    }

    @Override
    public void setY(float y)
    {
        this.y = y > max_Y ? max_Y : y < 0 ? 0 : y;
    }

    @Override
    public float getCenterBitmapX()
    {
        return centerBitmapX + x;
    }

    @Override
    public float getCenterBitmapY()
    {
        return centerBitmapY + y;
    }
    	
	public void reset()
    {
        x = screenWidth / 20;
        y = screenHeight / 2;
        health = 3;
    }

	public int getHealth()
	{
		return health;
	}
	
}
